﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_favorites : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private Tools t = new Tools();
	private int page;
	private int currentpage;
	private long amount;

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(Session.Count > 0 
		&& ap.CheckSessionExist(Session["Zappr"].ToString())) {
			Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());

			int line = 0;
			List<ImageItem> items = new List<ImageItem>();
			TableRow row = null;

			// avatar
			HyperLink av = new HyperLink();
			av.CssClass = "avatarImg";
			//av.NavigateUrl = "~/default.aspx?id=images&user=" + acc.Username;
			av.NavigateUrl = "~/images/" + acc.Username + "/default.aspx";

			if(acc.Avatar != "" && acc.Avatar != null) {
				av.ImageUrl = "~/Avatar/" + acc.Avatar;
			}
			else {
				av.ImageUrl = "~/App_Themes/Default/Images/avatar_placeholder.jpg";
			}

			userAvatar.Controls.Add(av);
			userAvatar.Controls.Add(new LiteralControl(""));

			// first slash
			Label litSlashOne = new Label();
			litSlashOne.CssClass = "txtSmallGreen";
			litSlashOne.Text = "/";

			userText.Controls.Add(litSlashOne);

			// link dashboard
			Label linkDash = new Label();
			linkDash.CssClass = "txtH2";
			linkDash.Text = GetLocalResourceObject("Profile").ToString();

			userText.Controls.Add(linkDash);

			// second slash
			Label litSlashTwo = new Label();
			litSlashTwo.CssClass = "txtSmallGreen";
			litSlashTwo.Text = "/";

			userText.Controls.Add(litSlashTwo);

			// link user
			Label linkUser = new Label();
			linkUser.CssClass = "txtH2";
			linkUser.Text = acc.Username;

			userText.Controls.Add(linkUser);

			// pages
			if(Request.QueryString.Get("page") == null) {
				page = 0;
				currentpage = 1;
			}
			else {
				page = Convert.ToInt32(Request.QueryString.Get("page").ToString());
				currentpage = page;
				page = t.ConvertPageToStartValue(page, 32);
			}

			// get images
			amount = ip.GetFavoriteImageAmount(acc.ID);

			if(page == 0) {
				items = ip.GetFavoritesList(acc.ID, page, 32, 32, true);
			}
			else {
				items = ip.GetFavoritesList(acc.ID, page + 32, 32, 32, true);
			}

			// list images
			foreach(ImageItem item in items) {
				if(line == 0) {
					row = new TableRow();
					row.Attributes.Add("align", "left");
				}

				//
				// image
				//

				TableCell cell = new TableCell();
				cell.VerticalAlign = VerticalAlign.Top;
				cell.HorizontalAlign = HorizontalAlign.Left;
				cell.Width = new Unit(75);
				cell.Style.Add("width", "75px");

				// image
				HyperLink link = new HyperLink();
				link.ImageUrl = "../../Images/" + item.Image75x75;
				link.CssClass = "listTitle";
				//link.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
				link.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";
				link.Text = item.Name;

				cell.Controls.Add(link);

				// newline
				cell.Controls.Add(new LiteralControl("<br />"));

				row.Cells.Add(cell);
				imageTable.Rows.Add(row);

				//
				// compute
				//
				line++;

				if(line == 6) {
					row = null;

					line = 0;
				}
			}

			if(amount > 32 || page > 0) {
				this.DisplayPageList(Convert.ToInt32(amount));
			}
		}
		else {
			Response.Redirect("~/default.aspx");
		}
	}

	/// <summary>
	/// Display the pages on the footer
	/// </summary>
	/// <param name="amount"></param>
	private void DisplayPageList(int amount) {
		long pages = (Convert.ToInt64(amount) / 32) + 1;
		int counter = 0;

		// pages
		HyperLink link = null;

		if(pages > 1) {
			plcFooter.Controls.Add(new LiteralControl("<br />"));

			if(currentpage != 1) {
				link = null;
				link = new HyperLink();
				link.CssClass = "pageStep";
				link.NavigateUrl = "~/default.aspx?id=favorites";
				link.NavigateUrl += "&page=" + (currentpage - 1).ToString();
				link.Text = GetLocalResourceObject("Previous").ToString();

				plcFooter.Controls.Add(link);
			}

			for(int i = 1; i <= pages; i++) {
				link = new HyperLink();

				if(i == currentpage) {
					link.CssClass = "pageCurrentStep";
				}
				else {
					link.CssClass = "pageStep";
				}

				link.NavigateUrl = "~/default.aspx?id=favorites";
				link.NavigateUrl += "&page=" + i.ToString();
				link.Text = i.ToString();

				// aktiver link ist "page"

				plcFooter.Controls.Add(link);

				plcFooter.Controls.Add(new LiteralControl("&nbsp;"));

				link = null;
				counter++;
			}

			if(currentpage < counter) {
				link = null;
				link = new HyperLink();
				link.CssClass = "pageStep";
				link.NavigateUrl = "~/default.aspx?id=favorites";
				link.NavigateUrl += "&page=" + (currentpage + 1).ToString();
				link.Text = GetLocalResourceObject("Next").ToString();

				plcFooter.Controls.Add(link);
			}
		}
	}
}
